package com.czk.tx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/8 14:20
 */
@Service
public class SellService {

    @Autowired
    private SellService sellService;

    @Transactional
    public void sellProduct() {
        try {
            //  Transaction rolled back because it has been marked as rollback-only
            sellService.sellProductBiz();
        } catch (Exception e) {
            System.out.println("发生异常" + e.getMessage());
        }
        System.out.println("执行成功");
    }

    @Transactional
    public void sellProductBiz() {
        // 模拟异常
        int i = 1/0;
    }
    @Transactional
    public void sellProduc_1t() {
        try {
            // 此时不会抛出rollback-only异常
            sellService.sellProductBiz_1();
        } catch (Exception e) {
            System.out.println("发生异常" + e.getMessage());
        }
        System.out.println("执行成功");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW) // 指定新起一个事务，则不会影响前面的事务
    public void sellProductBiz_1() {
        // 模拟异常
        int i = 1/0;
    }
}
